package models

import "gorm.io/gorm"

type UserAppApi struct {
	AppId   string `json:"appId"`
	ApiPath string `json:"apiPath"`
}

func (UserAppApi) TableName() string {
	return "sys_user_app_api_r"
}

func (u *UserAppApi) BatchInsert(db *gorm.DB, appId string, apiPath []string) (err error) {
	//当前对象
	apiList := formatToArray(appId, apiPath)
	//批量新增
	err = db.Model(&UserAppApi{}).Create(apiList).Error
	//是否成功
	return err
}

func (u *UserAppApi) DelByAppId(db *gorm.DB, appId string) {
	//数据删除
	ud := UserAppApi{}
	db.Model(&ud).Where("app_id = ?", appId).Delete(ud)
}

func (u *UserAppApi) DelByAppIds(db *gorm.DB, appIdList []string) {
	//数据删除
	ud := UserAppApi{}
	db.Model(&ud).Where("app_id in (?)", appIdList).Delete(ud)
}

func formatToArray(appId string, apiPath []string) []UserAppApi {
	apiList := make([]UserAppApi, 0)
	for _, val := range apiPath {
		apiItem := UserAppApi{}
		apiItem.AppId = appId
		apiItem.ApiPath = val
		apiList = append(apiList, apiItem)
	}
	return apiList
}
